﻿using Coolzon.Common;
using Coolzon.Web.Models;
using Coolzon.WebServer.Code.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.WebServer.Code.ApiControllers
{
    public class SolutionController : ApiController
    {
        [HttpGet]
        public ServiceResult<List<SolutionAlbum>> AlbumList()
        {
            List<SolutionAlbum> list = new List<SolutionAlbum>();
            for (int index = 0; index < ServerData.Instance.SolutionList.Count; index++)
            {
                list.Add(new SolutionAlbum()
                {
                    Name = ServerData.Instance.SolutionList[index].Name,
                    Caption = ServerData.Instance.SolutionList[index].Caption,
                    Version = ServerData.Instance.SolutionList[index].Version.ToString(),
                    Image = "http://localhost:8081//images/solution1.jpg"
                });
            }
            return new ServiceResult<List<SolutionAlbum>>(list);
        }

        [HttpGet]
        public ServiceResult<List<RT.SolutionFile>> FileList(string solution, string localVersion)
        {
            Version oLocalVersion;
            if(!string.IsNullOrEmpty(localVersion) && Version.TryParse(localVersion, out oLocalVersion))
            {
                for (int index = 0; index < ServerData.Instance.SolutionList.Count; index++)
                {
                    if (ServerData.Instance.SolutionList[index].Version <= oLocalVersion)
                    {
                        // 服务器上的版本小于或和本地版本一样不用同步了
                        return new ServiceResult<List<RT.SolutionFile>>(null);
                    }
                }
            }

            for(int index =0; index < ServerData.Instance.Solutions.Count; index++)
            {
                if(string.Equals(ServerData.Instance.Solutions[index].Name, solution, StringComparison.CurrentCultureIgnoreCase))
                {
                    return new ServiceResult<List<RT.SolutionFile>>(ServerData.Instance.Solutions[index].Files);
                }
            }
            return new ServiceResult<List<RT.SolutionFile>>(false, 1, "没有发现方案");
        }

        [HttpGet]
        public HttpResponseMessage Download(string solution, string relativePath)
        {
            try
            {
                var filePath = Config.GetSolutionContentFile(solution, relativePath);
                Logger.DebugToTag("webapi", string.Format("download:{0}", filePath));
                var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = new FileInfo(filePath).Name
                };
                return response;
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.ExpectationFailed);
            }
        }
    }
}